package com.drawu.drawuspring.controller;


import com.drawu.drawuspring.domain.DTO.CommentDTO;
import com.drawu.drawuspring.domain.DTO.PostDTO;
import com.drawu.drawuspring.domain.Result;
import com.drawu.drawuspring.domain.VO.CommentVO;
import com.drawu.drawuspring.service.ICommentsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author hui
 * @since 2024-10-14
 */
@Slf4j
@RestController
@RequestMapping("/comments")
@RequiredArgsConstructor
@Tag(name = "评论相关接口", description = "评论相关接口")
public class CommentsController {

    private final ICommentsService commentsService;


    @PostMapping("/reply")
    @Operation(summary = "AI自动回复评论", description = "AI自动回复评论")
    public Result replyComment(@RequestBody PostDTO dto) throws Exception {
        return Result.success(commentsService.replyComment(dto));
    }

    @PostMapping("/save")
    @Operation(summary = "添加评论", description = "添加评论")
    public Result addComment(@RequestBody CommentDTO dto) {
        return Result.success(commentsService.addComment(dto));
    }


    @GetMapping("/get")
    @Operation(summary = "获取评论列表", description = "前端把当前评论id赋值给repliedCommentId作为子评论的fatherid")
    public Result<List<CommentVO>> getCommentsByPostId(@RequestParam("type") Integer type, @RequestParam("postId") Integer postId) {
        return Result.success(commentsService.getCommentsByPostId(type, postId));
    }

    @GetMapping("/count/{postId}")
    @Operation(summary = "获取评论数量", description = "获取评论数量")
    public Result<Integer> getCommentCountByPostId(@PathVariable("postId") Integer postId) {
        return Result.success(commentsService.getCommentCountByPostId(postId));
    }

    @GetMapping("/delete")
    @Operation(summary = "删除评论", description = "删除评论")
    public Result deleteComment(@RequestParam("postId") Integer postId, @RequestParam("commentId") Integer commentId) {
        return Result.success(commentsService.deleteComment(postId,commentId));
    }

    // 上传图片
    @Operation(summary = "上传帖子图片")
    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam("file") MultipartFile mpFile) throws IOException {
        return commentsService.uploadImage(mpFile);
    }
}
